package com.ruoyi.web.controller.tiebauser.controller;

import java.io.IOException;
import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.generator.service.IGenTableService;
import com.ruoyi.web.controller.tiebauser.domain.TiebaUsers;
import com.ruoyi.web.controller.tiebauser.params.TiebaUserLoginParam;
import com.ruoyi.web.controller.tiebauser.service.ITiebaUsersService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 贴吧用户Controller
 *
 * @author yong
 * @date 2025-03-09
 */
@Api(tags = "用户-贴吧用户")
@RestController
@RequestMapping("/tiebauser/tiebauser")
public class TiebaUsersController extends BaseController {
    @Autowired
    private ITiebaUsersService tiebaUsersService;

    @Autowired
    private IGenTableService genTableService;


    /**
     * 查询贴吧用户列表
     */
    @ApiOperation("管理员-查询贴吧用户列表")
//    @PreAuthorize("@ss.hasPermi('tiebauser:tiebauser:list')")
    @GetMapping("/list")
    public TableDataInfo list(TiebaUsers tiebaUsers) {
        startPage();
//        tiebaUsers.setDeptId(getDeptId());
        List<TiebaUsers> list = tiebaUsersService.selectTiebaUsersList(tiebaUsers);
        return getDataTable(list);
    }

    /**
     * 获取贴吧用户详细信息
     */
    @ApiOperation("用户信息")
//    @PreAuthorize("@ss.hasPermi('tiebauser:tiebauser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tiebaUsersService.getById(id));
    }

    /**
     * 新增贴吧用户
     */
    @ApiOperation("管理员-用户-注册用户")
//    @PreAuthorize("@ss.hasPermi('tiebauser:tiebauser:add')")
    @Log(title = "贴吧用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TiebaUsers tiebaUsers) {
        tiebaUsers.setDeptId(103L);
        TiebaUsers tiebaUser = tiebaUsersService.getByUserName(tiebaUsers.getUsername());
        if (tiebaUser != null) {
            return AjaxResult.success(tiebaUser);
        }
        return toAjax(tiebaUsersService.save(tiebaUsers));
    }

    /**
     * 修改贴吧用户
     */
    @ApiOperation("修改用户信息")
//    @PreAuthorize("@ss.hasPermi('tiebauser:tiebauser:edit')")
    @Log(title = "贴吧用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TiebaUsers tiebaUsers) {
        tiebaUsers.setDeptId(getDeptId());
        return toAjax(tiebaUsersService.updateById(tiebaUsers));
    }

    /**
     * 删除贴吧用户
     */
    @ApiOperation("删除用户信息")
//    @PreAuthorize("@ss.hasPermi('tiebauser:tiebauser:remove')")
    @Log(title = "贴吧用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tiebaUsersService.removeByIds(Arrays.asList(ids)));
    }


    /**
     * 删除贴吧用户
     */
    @ApiOperation("登录")
    @Log(title = "贴吧用户")
    @PostMapping("/tieba-login")
    public AjaxResult tiebaLogin(@RequestBody TiebaUserLoginParam loginParam) {
        String username = loginParam.getUsername();
        String password = loginParam.getPassword();
        TiebaUsers tiebaUsers = tiebaUsersService.tiebaLogin(username, password);
        System.out.println("贴吧用户登录" + tiebaUsers);
        return AjaxResult.success(tiebaUsers);
    }


    @GetMapping("/batchGenCode")
    @ApiOperation("代码生成框架")
    @Anonymous
    public void batchGenCode(HttpServletResponse response, String tables) throws IOException {
        String[] tableNames = Convert.toStrArray(tables);
        byte[] data = genTableService.downloadCode(tableNames);
        genCode(response, data);
    }

    /**
     * 生成zip文件
     */
    private void genCode(HttpServletResponse response, byte[] data) throws IOException {
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"tieba.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }
}
